<template>
  <div class="forum-container">
    <el-card class="post-list">
      <div class="post-cards">
        <el-card
          v-for="post in postList"
          :key="post.id"
          class="post-card"
          @click="handleViewDetail(post.id)"
        >
          <div class="card-content">
            <h3 class="post-title">{{ post.title }}</h3>
            <div class="post-meta">
<!--              <span class="author">{{ post.userName }}</span>-->
              <span class="time">{{ post.createTime }}</span>
            </div>
            <p class="post-excerpt">{{ post.content?.substring(0, 100) }}...</p>
          </div>
        </el-card>
      </div>

      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.size"
        :total="total"
        layout="total, prev, pager, next"
        @current-change="handlePageChange"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { faker } from '@faker-js/faker'
import {getPostPage} from "@/api/post.js";

const router = useRouter()

const postList = ref([])
const total = ref(0)
const queryParams = ref({
  page: 1,
  size: 10
})



const generateMockData = () => {
  return {
    id: faker.string.uuid(),
    title: faker.lorem.words(6),
    userName: faker.person.fullName(),
    createTime: faker.date.recent().toLocaleString(),
    content: faker.lorem.paragraphs(2)
  }
}

const fetchPostList = async () => {
  // if (import.meta.env.MODE === 'development') {
  //   postList.value = Array.from({ length: 10 }, generateMockData)
  //   total.value = 50
  //   return
  // }
  try {
    const res = await getPostPage(queryParams.value)
    postList.value = res.data.records
    total.value = res.data.total
    console.log(postList.value)
  } catch (error) {
    console.error('获取帖子列表失败:', error)
  }
}

const handleViewDetail = (id) => {
  router.push(`/forum/${id}`)
}

const handlePageChange = (newPage) => {
  queryParams.value.page = newPage
  fetchPostList()
}

onMounted(() => {
  fetchPostList()
})
</script>

<style scoped>
.forum-container {
  padding: 20px;
}
.post-cards {
  display: grid;
  gap: 16px;
}
.post-card {
  cursor: pointer;
  transition: box-shadow 0.3s;
}
.post-card:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}
.card-content {
  padding: 16px;
}
.post-title {
  font-size: 16px;
  margin-bottom: 8px;
  color: #333;
}
.post-meta {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}
.post-excerpt {
  font-size: 14px;
  color: #888;
  line-height: 1.5;
}

.el-pagination {
  margin-top: 20px;
  justify-content: center;

  :deep(.btn-prev),
  :deep(.btn-next) {
    padding: 0 12px;
    border-radius: 4px;
    transition: all 0.3s;
  }

  :deep(.number) {
    margin: 0 4px;
    border-radius: 4px;
    &.is-active {
      background: #409EFF;
      color: white;
    }
  }
}
</style>
